package com.tdk.bts.action;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import com.tdk.bts.entity.Film;
import com.tdk.bts.factory.ObjectFactory;
import com.tdk.bts.service.FilmService;
import com.tdk.bts.util.ExcelUtil;
import com.tdk.bts.util.Page;
import com.tdk.bts.util.Request;

public class FilmAction {

	/**
	 * 查询所有的电影信息
	 * 
	 * 请求参数 pageNo 查询页数 响应数据 Page<Film>类型的 filmList
	 * 
	 */
	public void findAllFilms(Request request) throws Exception {
		// 从request里面接收前台页面传过来的值,校验（不一定）
		int page = Integer.parseInt(request.getParameter("pageNo"));
		// 工厂加载代理类对象
		FilmService filmService = (FilmService) ObjectFactory
				.getObject("filmService");
		System.out.println(filmService);

		Page<Film> filmList = filmService.findAllFilms(page);
		System.out.println("电影列表："+filmList);
		for (int i = 0; i < filmList.size(); i++) {
			System.out.println((Film) filmList.get(i));
		}
		request.setAttribute("filmList", filmList);
	}

	/**
	 * 批量添加电影信息
	 * 
	 * 请求参数 file xls文件路径
	 * 
	 */
	public void batchAddFilms(Request request) throws Exception {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		String path = request.getParameter("file");
		List<Film> films = new ArrayList<Film>();
		Map<String,List<String>>map = ExcelUtil.importExcel(path);
		for(String id:map.keySet()){
			List<String> list = map.get(id);
			Film f = new Film();
			f.setId(Integer.parseInt(id));
			f.setName(list.get(0));
			f.setInfo(list.get(1));
			f.setPicUrl(list.get(2));
			String string = list.get(3);
			System.out.println("string:"+string);
			f.setPublishDate(df.parse(string));
			films.add(f);
		}
		FilmService filmservice = (FilmService)ObjectFactory.getObject("filmService");
		filmservice.addFilms(films);
	}

	/**
	 * 根据电影id删除电影
	 * 
	 * 请求参数 ids :表示要删除电影的全部id值,如："2,3,12"
	 * 
	 */
	public void deleteFilm(Request request) throws Exception {
		System.out.println("id的内容："+request.getParameter("ids"));
		FilmService filmService = (FilmService) ObjectFactory
				.getObject("filmService");
		filmService.removeFilms(request.getParameter("ids"));
	}

	
}
